package com.jeesit.cloud.spot.handler;

import com.jeesit.cloud.dto.JSONResult;
import com.jeesit.cloud.exception.BizException;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;

/**
 * @author syd
 * @version 0.0.1
 * @description 全局异常处理
 * @date 2023/1/12
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 自定义业务异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public JSONResult<Void> resolveBizException(BizException ex) {
        JSONResult errorWebResult = new JSONResult();
        errorWebResult.setSuccess(false);
        errorWebResult.setMessage(ex.getMessage());
        return errorWebResult;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public JSONResult<Void> resolveIllegalArgumentException(IllegalArgumentException ex) {
        JSONResult errorWebResult = new JSONResult();
        errorWebResult.setSuccess(false);
        errorWebResult.setMessage(ex.getMessage());
        ex.printStackTrace();
        return errorWebResult;
    }

    /**
     * 用来处理bean validation异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public JSONResult<Void> resolveConstraintViolationException(ConstraintViolationException ex) {
        JSONResult errorWebResult = new JSONResult();
        errorWebResult.setSuccess(false);
        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
        if (!CollectionUtils.isEmpty(constraintViolations)) {
            StringBuilder msgBuilder = new StringBuilder();
            for (ConstraintViolation constraintViolation : constraintViolations) {
                msgBuilder.append(constraintViolation.getMessage()).append(",");
            }
            String errorMessage = msgBuilder.toString();
            if (errorMessage.length() > 1) {
                errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
            }
            errorWebResult.setMessage(errorMessage);
            return errorWebResult;
        }
        errorWebResult.setMessage(ex.getMessage());
        return errorWebResult;
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public JSONResult<Void> resolveMethodArgumentNotValidException(MethodArgumentTypeMismatchException ex) {
        JSONResult errorWebResult = new JSONResult();
        errorWebResult.setSuccess(false);
        errorWebResult.setMessage(ex.getMessage());
        return errorWebResult;
    }

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public JSONResult<Void> resolveBindException(BindException ex) {
        JSONResult errorWebResult = new JSONResult();
        errorWebResult.setSuccess(false);
        List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
        if (!CollectionUtils.isEmpty(objectErrors)) {
            StringBuilder msgBuilder = new StringBuilder();
            for (ObjectError objectError : objectErrors) {
                msgBuilder.append(objectError.getDefaultMessage()).append(",");
            }
            String errorMessage = msgBuilder.toString();
            if (errorMessage.length() > 1) {
                errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
            }
            errorWebResult.setMessage(errorMessage);
            return errorWebResult;
        }
        errorWebResult.setMessage(ex.getMessage());
        return errorWebResult;
    }
}
